<template>
  <div class="good_list">
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item to="/home/welcome">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索框 -->
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <el-col :span="8">
        <el-input
          placeholder="请输入商品"
          class="input-with-select"
          v-model="queryinfo.query"
          clearable
          @clear="searchGood"
        >
          <el-button slot="append" icon="el-icon-search" @click="searchGood"></el-button>
        </el-input>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" style="margin-top:8px;margin-bottom:20px" @click="toAdd">添加商品</el-button>
      </el-col>
      <!-- 商品列表展示 -->
      <el-table :data="goodList" border style="width: 100%">
        <el-table-column type="index" label="#" width="100"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="380"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="120"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="120"></el-table-column>
        <el-table-column prop="add_time" label="添加时间" width="180">
          <template slot-scope="scope">{{scope.row.add_time|dateformat}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="small"
              type="primary"
              icon="el-icon-edit"
              @click="toEdit(scope.row.goods_id)"
            >编辑</el-button>
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              @click="deleteGood(scope.row.goods_id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页效果展示 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[2, 4, 6, 10]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 查询信息的参数
      queryinfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      total: 0,
      //  获取的商品信息
      goodList: [],

    }
  },
  methods: {
    // 获取商品数据
    async getGoods () {
      const res = await this.axios.get("goods", { params: this.queryinfo });
      this.goodList = res.data.goods;
      this.total = res.data.total;
    },
    // 页码大小发生变化
    handleSizeChange (newS) {
      this.queryinfo.pagesize = newS;
      this.getGoods();
    },
    // 页码发生变化
    handleCurrentChange (newP) {
      this.queryinfo.pagenum = newP;
      this.getGoods();
    },
    // 搜索框的搜索功能
    searchGood () {
      this.getGoods();
    },
    // 删除商品功能
    deleteGood (id) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          // 发送请求去删除数据
          this.axios.delete("goods/" + id).then(res => {
            if (res.meta.status == 200) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              // 重新获取数据
              this.getGoods();
            } else {
              this.$message.error({
                message: "删除失败",
                duration: 1000
              });
            }
          });
        })
        .catch(() => { });
    },
    // 跳转到添加商品页面
    toAdd () {
      this.$router.push('goods/addgood/0');
    },
    toEdit (id) {
      this.$router.push('goods/addgood/' + id);
    }
  },
  created () {
    this.getGoods();
  },
}
</script>

<style lang="scss" >
</style>